// Package account @program: gateway
//@author: edte
//@create: 2021-12-29 11:22
package account

import (
	"context"
	"errors"
	useraccount "gateway/config/account"
	"gateway/tools/admin"
	"gateway/tools/security"
	"github.com/gin-gonic/gin"
	"net/http"
)

var c2 = useraccount.NewAdminServerClient(conn)

// AdminLogin /*func AdminGetLink() (useraccount.AdminServerClient, error) {
func AdminLogin(username string, password string) (gin.H, error) {
	/*c, err := AdminGetLink()
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
	}
	cxt, cancel := context.WithTimeout(context.Background(), time.Second)
	cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()*/
	login, err := c2.AdminLogin(context.Background(), &useraccount.LoginAdminFormRequest{AdminPhone: username, AdminPassword: password})
	if err != nil {
		return gin.H{"status": http.StatusBadRequest}, err
	}
	user := login.UserInfo
	adminer := admin.UserAdmin{Id: int(user.UserId), AdminNum: user.UserPhone, AdminPD: user.UserPassword}
	token := security.GetAdminToken(adminer)
	h := gin.H{"status": login.Status, "message": "登录成功", "data": token}
	return h, err
}
func AdminRegister(token string, username string, password string, lever int) (gin.H, error) {
	admininfo, err := security.AdminVerifyAction(token)
	if err != nil {
		return gin.H{"status": http.StatusBadGateway, "message": err.Error()}, err
	}
	if admininfo.PrivilegeLevel > 3 {
		return gin.H{"status": http.StatusBadGateway, "message": errors.New("无权限创建账号")}, err
	}
	if admininfo.PrivilegeLevel > lever {
		return gin.H{"status": http.StatusBadGateway, "message": errors.New("越权创建账号")}, err
	}
	/*c, err := AdminGetLink()
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
	}*/
	/*cxt, cancel := context.WithTimeout(context.Background(), time.Second)
	cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()*/
	register, err := c2.AdminRegister(context.Background(),
		&useraccount.RegisterAdminFormRequest{
			AdminPhone:    username,
			AdminPassword: password,
			AdminLever:    int32(lever)})
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
	}
	h := gin.H{"status": register.Status, "message": "注册成功"}
	return h, err
}
func AdminQueryData(token string, startId int, endId int) (gin.H, error) {
	admininfo, err := security.AdminVerifyAction(token)
	if err != nil {
		return gin.H{"status": http.StatusBadGateway, "message": err.Error()}, err
	}
	if admininfo.PrivilegeLevel > 4 {
		return gin.H{"status": http.StatusBadGateway, "message": errors.New("无权限查询账号")}, err
	}
	/*c, err := AdminGetLink()
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
	}*/
	/*cxt, cancel := context.WithTimeout(context.Background(), time.Second)
	cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()*/
	data, err := c2.AdminQueryData(context.Background(), &useraccount.QueryUserInfoRequest{StartUserId: int32(startId), EndUserId: int32(endId)})
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
	}
	h := gin.H{"status": data.Status, "message": "查询成功", "data": data.UserForms}
	return h, err
}
func BlockUser(token string, userid int) (gin.H, error) {
	userinfo, err := security.AdminVerifyAction(token)
	if err != nil {
		return gin.H{"status": http.StatusBadGateway, "message": err.Error()}, err
	}
	if userinfo.PrivilegeLevel > 4 {
		return gin.H{"status": http.StatusBadGateway, "message": errors.New("无权限操作")}, err
	}
	/*c, err := AdminGetLink()
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
	}*/
	/*cxt, cancel := context.WithTimeout(context.Background(), time.Second)
	cxt, cancel := context.WithTimeout(context.Background(), time.Minute)
	defer cancel()*/
	user, err := c2.AdminBlockUser(context.Background(), &useraccount.BlockUserRequest{UserId: int32(userid)})
	if err != nil {
		return gin.H{"status": http.StatusBadRequest, "message": "could not greet: " + err.Error()}, err
	}
	delete(security.UserMap, userid)
	h := gin.H{"status": user.Status, "message": "操作成功"}
	return h, err
}
